<template>
  <el-card class="box">
    <!-- 查询区域 -->
    <el-form :inline="true" :model="Form" class="demo-form-inline">
      <el-form-item label="文章ID">
        <el-input v-model="Form.number" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="文章标题">
        <el-input v-model="Form.title" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="作者">
        <el-input v-model="Form.user" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item label="分类">
        <el-select v-model="Form.category" placeholder="请选择标签" clearable>
          <el-option label="分类一" value="分类一"></el-option>
          <el-option label="分类二" value="分类二"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          @click="Search_data(Form)"
          >查询</el-button
        >
      </el-form-item>
    </el-form>
    <!-- 表格区域 -->
    <el-table :data="sys_search" border stripe style="width: 100%">
      <el-table-column
        fixed
        type="selection"
        width="36"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        prop="number"
        label="文章ID"
        width="100"
      ></el-table-column>
      <el-table-column prop="title" label="文章标题"></el-table-column>
      <el-table-column
        prop="score"
        label="评分"
        sortable
        align="center"
        width="180"
      >
        <template v-slot="scope">
          <el-rate v-model="scope.row.score" disabled></el-rate>
        </template>
      </el-table-column>
      <el-table-column prop="schedule" label="进度" sortable>
        <template v-slot="scope">
          <el-progress
            :percentage="scope.row.schedule"
            :status="status(scope.row.schedule)"
          ></el-progress>
        </template>
      </el-table-column>
      <el-table-column prop="user" label="作者" width="120"></el-table-column>
      <el-table-column
        prop="category"
        label="分类"
        width="100"
      ></el-table-column>
      <el-table-column prop="date" label="发布日期"></el-table-column>
      <el-table-column prop="status" label="状态" sortable width="100">
        <template v-slot="scope">
          <el-tag size="small" v-if="scope.row.status == 0">已发布</el-tag>
          <el-tag type="warning" size="small" v-else-if="scope.row.status == 1"
            >修改中</el-tag
          >
          <el-tag type="info" size="small" v-else>未发布</el-tag>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="200">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-edit"
            @click="ArticleEdit(scope.row.number)"
            >编辑</el-button
          >
          <el-button
            type="danger"
            size="mini"
            icon="el-icon-delete"
            @click="ArticleDelete(scope.row.number)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 编辑框 -->
    <el-dialog
      title="编辑"
      :visible.sync="dialogVisible"
      width="30%"
      center
      :before-close="handleClose"
    >
      <el-form :model="EditForm">
        <el-form-item label="标题" label-width="80px">
          <el-input v-model="EditForm.title" width="100px"></el-input>
        </el-form-item>
        <el-form-item label="难度" label-width="80px">
          <el-rate v-model="EditForm.difficulty"></el-rate>
        </el-form-item>
        <el-form-item label="进度" label-width="80px">
          <el-slider v-model="EditForm.schedule" :step="10"> </el-slider>
        </el-form-item>
        <el-form-item label="归属" label-width="80px">
          <el-select v-model="EditForm.receive" placeholder="请选择归属者">
            <el-option label="月老" value="月老"></el-option>
            <el-option label="李白" value="李白"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发布" label-width="80px">
          <el-select v-model="EditForm.submit" placeholder="请选择发布者">
            <el-option label="chalk" value="chalk"></el-option>
            <el-option label="张三" value="张三"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发布日期" label-width="80px">
          <el-date-picker
            v-model="EditForm.date"
            type="date"
            placeholder="选择日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="状态" label-width="80px">
          <el-radio-group v-model="EditForm.status">
            <el-radio label="0">待接取</el-radio>
            <el-radio label="1">进行中</el-radio>
            <el-radio label="2">已完成</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </el-card>
</template>

<script>
import { sys } from '../api/json.json'
export default {
  data() {
    return {
      dialogVisible: false,
      EditForm: {},
      Form: {
        number: '',
        title: '',
        user: '',
        caregory: ''
      },
      search: {
        number: '',
        title: '',
        user: '',
        caregory: ''
      },
      sys: sys
    }
  },
  methods: {
    // 进度条
    status(e) {
      if (e === 100) {
        return 'success'
      }
    },
    Search_data(e) {
      this.search = e
    },
    // 查看按钮点击事件 路由跳转
    ArticleEdit(e) {
      this.$router.push({ path: `/content/article/${e}` })
      // this.$router.push({ name: 'TaskDetails', params: { details: e } })
    },
    // 编辑按钮点击事件
    edit(e) {
      this.EditForm = e
      this.dialogVisible = true
    },
    // 删除点击事件
    async ArticleDelete(e) {
      await this.$confirm(
        '此操作将永久删除该用户, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
        .then(_ => {
          // const res = this.$http.post('delete.php', e)
          const res = 200
          if (res === 200) {
            return this.$message.success('删除成功！')
          }
        })
        .catch(_ => {})
        // 重新从数据库获取一下数据
        // this.sys
    },
    // 编辑对话框关闭事件
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => { })
    }
  },
  computed: {
    sys_search() {
      return this.sys.filter(data => {
        return Object.keys(this.search).reduce((flag, item) => {
          if (!flag) {
            return false
          } else {
            return this.search[item].trim() ? String(data[item]).indexOf(this.search[item]) !== -1 : true
          }
        }, true)
      })
      // return this.sys.filter(data => (data.number.toLowerCase().includes(this.search.number.toLowerCase()) && data.title.toLowerCase().includes(this.search.title.toLowerCase())))
    }
  }
}
</script>
<style lang="less" scoped>
.el-card.box {
  height: 100%;
  span {
    font-size: auto;
  }
  .el-dialog {
    text-align: left;
    .el-rate {
      display: inline-block;
    }
  }
}
</style>
